package priv.dajie.tzzq.common;

import priv.dajie.tzzq.bot.model.TradeState;
import priv.dajie.tzzq.model.QualityName;
import priv.dajie.tzzq.spider.model.Platform;

import java.io.*;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class Tools {

    private static final String[] qualitys = {"普通", "铭刻", "纯正", "尊享", "亲笔签名", "融合", "独特", "吉祥", "冥灵", "英雄传世", "冻人", "凶煞", "上古", "基础", "绝版", "标准", "社区", "Valve", "自制", "自定义", "完整", "青睐", "融合"};
    private static final HashSet<String> hQualitys = new HashSet<String>();
    private static HashMap<String,TradeState> c5BuyState = new HashMap<>();
    private static HashMap<String, TradeState> buffBuyState = new HashMap<>();
    private static HashMap<String , Platform> platformCN = new HashMap<>();


    static {
        hQualitys.addAll(Arrays.asList(qualitys));

        c5BuyState.put("交易成功", TradeState.SUCCESS);
        c5BuyState.put("Deal success", TradeState.SUCCESS);
        c5BuyState.put("The deal is successful", TradeState.SUCCESS);
        c5BuyState.put("交易取消", TradeState.FAIL);
        c5BuyState.put("等待卖家发货", TradeState.WAIT);
        c5BuyState.put("系统发货中", TradeState.WAIT);
        c5BuyState.put("等待系统自动发货", TradeState.WAIT);
        c5BuyState.put("等待发货", TradeState.WAIT);
        c5BuyState.put("The deal has been cancelled", TradeState.FAIL);
        c5BuyState.put("正在发货", TradeState.WAIT);
        c5BuyState.put("待收货", TradeState.WAIT);
        c5BuyState.put("待处理", TradeState.WAIT);
        c5BuyState.put("处理中", TradeState.WAIT);
        c5BuyState.put("待发货", TradeState.WAIT);
        c5BuyState.put("发货中", TradeState.WAIT);
        c5BuyState.put("等待收货", TradeState.TAKE_BACK);
        c5BuyState.put("收货中", TradeState.WAIT);


        buffBuyState.put("购买成功", TradeState.SUCCESS);
        buffBuyState.put("购买失败-已退款", TradeState.FAIL);
        buffBuyState.put("等待卖家发货", TradeState.WAIT);
        buffBuyState.put("卖家发货中", TradeState.WAIT);
        buffBuyState.put("交易暂挂", TradeState.WAIT);
        buffBuyState.put("等待付款", TradeState.WAIT);
        buffBuyState.put("结算中", TradeState.WAIT);
        buffBuyState.put("订单状态确认中", TradeState.WAIT);
        buffBuyState.put("支付失败", TradeState.FAIL);

        platformCN.put("buff", Platform.BUFF);
        platformCN.put("c5", Platform.C5);
        platformCN.put("steam", Platform.STEAM);
    }

    public static QualityName getQualityName(String qName) {
        String[] names = qName.split(" ", 2);
        if (names.length > 1) {
            for (String quality : hQualitys) {
                if (names[0].equals(quality)) {
                    return new QualityName(names[0], names[1]);
                }
            }
        }
        return new QualityName("基础", qName);
    }

    public static byte[] readInputStream(InputStream inStream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    public static TradeState c5BuyStateParse(String state){
        TradeState tradeState = c5BuyState.get(state);
        if(tradeState == null)
            tradeState = TradeState.OTHER;
        return tradeState;
    }

    public static TradeState buffBuyStateParse(String state){
        TradeState tradeState = buffBuyState.get(state);
        if(tradeState == null)
            tradeState = TradeState.OTHER;
        return tradeState;
    }

    public static Platform platform(String platform){
        return platformCN.get(platform);
    }

    public static String toHex(byte[] arr) {
        StringBuffer md5str = new StringBuffer();
        int digital;
        for (int i = 0; i < arr.length; i++) {
            digital = arr[i];
            if (digital < 0) {
                digital += 256;
            }
            if (digital < 16) {
                md5str.append("0");
            }
            md5str.append(Integer.toHexString(digital));
        }
        return md5str.toString().toLowerCase();
    }

    public static String md5(String src) {
        String md5str = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] input = src.getBytes();
            byte[] buff = md.digest(input);
            md5str = toHex(buff);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5str;
    }

    public static synchronized long loadLastRunTime(){
        long lasttime = 0;
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(System.getProperty("user.dir")+ "/data/steamSpiderRunTime");
            bufferedReader = new BufferedReader(fileReader);
            String timeStr = bufferedReader.readLine();
            lasttime = Long.valueOf(timeStr);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileReader!=null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return lasttime;
    }

    public static synchronized void saveLastRunTime(long lasttime){
        FileWriter fileWriter=null;
        try {
            fileWriter = new FileWriter(System.getProperty("user.dir")+ "/data/steamSpiderRunTime");
            fileWriter.write(lasttime+"");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileWriter!=null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        saveLastRunTime(10);
        System.out.println(loadLastRunTime());
    }
}
